package com.fita.admin.entity.search.client;

import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.admin.entity.search.AbstractSearch;
import com.fita.entity.client.ClientType;

import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/6/26
 */
public class ClientSearch extends AbstractSearch {

    private List<ClientType> types;

    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        if (filter == null || refresh) {

            filter = new QueryFilter();

            filter.addSentence(":types IN ELEMENTS(types)" ,"types", types);
        }
        if (order == null){
            order = new QueryOrder();
            order.add("id", "DESC");
        }
        return filter;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    public String buildSentenceTemplate() {
        return "FROM Client WHERE disabled = false " + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
    }

    public List<ClientType> getTypes() {
        return types;
    }

    public void setTypes(List<ClientType> types) {
        this.types = types;
    }
}
